package com.springMVCStudy.api;

import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api")
@ResponseBody
public class APIController {

    // ServletContext：1、可以读取全局最大的配置；2、它是全局最大共享域；3、它是核心api
    @Autowired
    private ServletContext servletContext;

    public String data(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

        // 直接使用原生对象即可，自动装配
        // 方案一：通过request和session获取
        servletContext = request.getServletContext();
        servletContext = session.getServletContext();
        // 方案二：如上，@Autowired直接全局注入


        return servletContext.getContextPath();
    }

}
